package com.example.demo.service.impl;

import com.example.demo.domain.Article;
import com.example.demo.enums.StatusEnum;
import com.example.demo.repository.ArticleRepository;
import com.example.demo.service.ArticleService;
import com.example.demo.service.BaseService;
import com.example.demo.web.rest.evt.SaveArticleEvt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * @Author zhengmf
 * @Date 2019/1/4  15:20
 */
@Service
@Transactional
public class ArticleServiceImpl extends BaseService implements ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public void saveArticle(SaveArticleEvt evt) {
        Article helpArticle = new Article();
        BeanUtils.copyProperties(evt,helpArticle);
        if(helpArticle.getArticleId() == null) {
            helpArticle.setCreateUser(getCurrentUserAlias());
            helpArticle.setCreateTime(new Date());
        }
        helpArticle.setModifyUser(getCurrentUserAlias());
        helpArticle.setModifyTime(new Date());
        helpArticle.setStatus(StatusEnum.E.name());
        articleRepository.save(helpArticle);
    }

    @Override
    public void updateStatus(Long id) {
        articleRepository.updateStatus(StatusEnum.D.name(),id);
    }

}
